/*
 * widget draw
 */

#include "widget_draw.h"

#if defined(GKT_CONFIG_SCREEN_ENABLE) && GKT_CONFIG_SCREEN_ENABLE

void widget_draw_background(gkt_widget_s *widget, 
				gkt_rect_s *rect, int focus, int update)
{
	const gkt_rect_s *real_rect;
	uint32_t color;

	real_rect = rect ? rect : GKT_WIDGET_RECT_PTR(widget);
	color = focus ? GKT_WIDGET_FG_COLOR(widget) : 
				GKT_WIDGET_BG_COLOR(widget);
	gkt_screen_fill(real_rect, color, update);
}

void widget_draw_underline(gkt_widget_s *widget, 
				int update)
{
	if (GKT_WIDGET_HEIGHT(widget) > 2) {
		gkt_screen_draw_hline(GKT_WIDGET_X(widget), 
				GKT_WIDGET_Y(widget) + GKT_WIDGET_HEIGHT(widget) - 1, 
				GKT_WIDGET_WIDTH(widget), GKT_WIDGET_FG_COLOR(widget), 
				update);
	}
}

void widget_draw_border(gkt_widget_s *widget, 
				int update)
{
	if (__LIKELY((GKT_WIDGET_HEIGHT(widget) > 2) 
		&& (GKT_WIDGET_WIDTH(widget) > 1))) 
	{
		gkt_screen_draw_hline(GKT_WIDGET_X(widget), GKT_WIDGET_Y(widget), 
				GKT_WIDGET_WIDTH(widget), GKT_WIDGET_FG_COLOR(widget), 
				update);
		gkt_screen_draw_hline(GKT_WIDGET_X(widget), 
				GKT_WIDGET_Y(widget) + GKT_WIDGET_HEIGHT(widget) - 1, 
				GKT_WIDGET_WIDTH(widget), GKT_WIDGET_FG_COLOR(widget), 
				update);
		gkt_screen_draw_vline(GKT_WIDGET_X(widget), GKT_WIDGET_Y(widget) + 1, 
				GKT_WIDGET_HEIGHT(widget) - 2, GKT_WIDGET_FG_COLOR(widget), 
				update);
		gkt_screen_draw_vline(GKT_WIDGET_X(widget) + GKT_WIDGET_WIDTH(widget) - 1, 
				GKT_WIDGET_Y(widget) + 1, GKT_WIDGET_HEIGHT(widget) - 2, 
				GKT_WIDGET_FG_COLOR(widget), update);
	}
}

void widget_draw_text(gkt_widget_s *widget, 
				gkt_rect_s *rect, const char *text, 
				uint32_t character_nums, 
				int focus, int update)
{
	gkt_font_text_info_s text_info;
	gkt_font_draw_params_s draw_params;
	gkt_rect_s text_rect;

	if (text) {
		/* init draw attributes */
		draw_params.size = widget->desc->font_size;
		if (focus) {
			draw_params.fg_color = GKT_WIDGET_BG_COLOR(widget);
			draw_params.bg_color = GKT_WIDGET_FG_COLOR(widget);
		}
		else {
			draw_params.fg_color = GKT_WIDGET_FG_COLOR(widget);
			draw_params.bg_color = GKT_WIDGET_BG_COLOR(widget);
		}
		draw_params.x_space = GKT_WIDGET_TEXT_X_SPACE;
		draw_params.update = update;

		/* init rect */
		if (!rect) {
			text_rect = GKT_WIDGET_RECT(widget);
			if (widget->flags & GKT_WIDGET_F_BORDER) {
				text_rect.x += GKT_WIDGET_BORDER_WIDTH + GKT_WIDGET_TEXT_X_DIST;
				text_rect.y += GKT_WIDGET_BORDER_WIDTH;
				text_rect.width -= (GKT_WIDGET_BORDER_WIDTH + GKT_WIDGET_TEXT_X_DIST) * 2;
				text_rect.height -= GKT_WIDGET_BORDER_WIDTH * 2;
			}
			else {
				text_rect.x += GKT_WIDGET_TEXT_X_DIST;
				text_rect.width -= GKT_WIDGET_TEXT_X_DIST * 2;
			}
		}
		else
			text_rect = *rect;

		/* caculate total character nums and text width */
		text_info.character_nums = character_nums;
		gkt_font_get_text_info(text, widget->desc->font_size, &text_info);
#if (GKT_WIDGET_TEXT_X_SPACE > 0)
		if (text_info.character_nums > 1)
			text_info.total_width += (text_info.character_nums - 1) * GKT_WIDGET_TEXT_X_SPACE;
#endif
		/* horizontal alignment */
		if (text_info.total_width < (uint32_t)text_rect.width) {
			if (GKT_WIDGET_F_ALIGN_MIDDLE == (widget->flags & GKT_WIDGET_F_ALIGN_HMASK))
				text_rect.x += (text_rect.width - (uint16_t)text_info.total_width) >> 1;
			else if (GKT_WIDGET_F_ALIGN_RIGHT == (widget->flags & GKT_WIDGET_F_ALIGN_HMASK))
				text_rect.x += text_rect.width - (uint16_t)text_info.total_width;

			text_rect.width = (uint16_t)text_info.total_width;
		}

		/* vertical alignment */
		if ((uint16_t)widget->desc->font_size < text_rect.height) {
			if (GKT_WIDGET_F_ALIGN_CENTER == (widget->flags & GKT_WIDGET_F_ALIGN_VMASK))
				text_rect.y += (text_rect.height - widget->desc->font_size) >> 1;
			else if (GKT_WIDGET_F_ALIGN_BOTTOM == (widget->flags & GKT_WIDGET_F_ALIGN_VMASK))
				text_rect.y += text_rect.height - widget->desc->font_size;

			text_rect.height = (uint16_t)widget->desc->font_size;
		}

		/* draw text */
		gkt_font_draw_text(text, &text_rect, &draw_params);
	}
}

#endif

